/**
 * 
 */
package edu.lnu.project.timeeditapp.helper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.lnu.project.timeeditapp.R;

import edu.lnu.project.timeeditapp.db.TimeEditDataSource;
import edu.lnu.project.timeeditapp.parser.Event;


@SuppressWarnings("deprecation")
public class MyHelper {

	public static String USERNAME;
	public static final String SILENTMODE = "silentMode";
	public static final String COLOR = "color";
	public static final String VISIBILITY = "visibility";
	public static final String COURSENAME = "courseName";
	public static final String COURSELISTKEY = "courseList";

	public static ArrayList<Integer> yearsWith53Weeks = new ArrayList<Integer>( 
			Arrays.asList( 2004, 2009, 2015, 2020, 2026, 2032, 2037, 2043, 2048, 
					2054, 2060, 2065, 2071, 2076, 2082, 2088, 2093, 2099, 2105, 2111, 2116, 2122,
					2128, 2133, 2139, 2144, 2150, 2156, 2161, 2167, 2172, 2178, 2184, 2189, 2195 ));
	public static ArrayList<String> eventsNamesList;
	public static ArrayList<HashMap<String, String>> visibilityArray = new ArrayList<HashMap<String, String>>();
	public static TimeEditDataSource datasource;
	public static ArrayList<Integer> colors = new ArrayList<Integer>();
	public static int currentWeek;
	
	public static SharedPreferences visibilityColorPreferences;
	public static  SharedPreferences.Editor visibilityColorPreferencesEditor;
	
	
	/**
	 * Returns the current week
	 * @return int (Current week)
	 */
	public static int getCurrentWeek(){

		//Get current date and time
		Date todaysDate = new Date();

		//Create an instance of calendar
		Calendar calendar = Calendar.getInstance();

		//Set calender to current day
		calendar.setTime(todaysDate);

		//Return the current week
		return calendar.get(Calendar.WEEK_OF_YEAR);

	}

	/**
	 * 
	 * @param int ( weeks to add or remove )
	 * @return int ( the new week number ) 
	 */
	public static int changeWeek(int currentWeek, int add){

		//By adding zero we return the week you put in
		if( add == 0 ){
			return currentWeek;
		}
		//If its the last week of the year
		else if( isLastWeekOfYear(currentWeek) && add > 0 ){
			return 1;
		}
		//If its the first week of the year
		else if( currentWeek == 1 && add < 0){
			return getLastWeekOfYear();
		}

		else{
			return currentWeek + add;
		}
	}

	/**
	 * Check if a week is the last week of the year
	 * @param int ( the week that should be check )
	 * @return boolean ( true if its the last week of the year )
	 */
	public static boolean isLastWeekOfYear( int week ){
		Date date = new Date();

		if( yearsWith53Weeks.contains(date.getYear()) ) {
			return week == 53;
		}
		else{
			return week == 52;
		}
	}

	/**
	 * Returns the last week of the year
	 * @return int ( the last week of the year )
	 */
	public static int getLastWeekOfYear(){
		Date date = new Date();

		if( yearsWith53Weeks.contains(date.getYear()) ) {
			return 53;
		}
		else{
			return 52;
		}
	}

	public static String getTodaysDate(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//get current date time with Date()
		Date date = new Date();
		return dateFormat.format(date).toString();
	}

	//return a day name from a given date (eg. Monday, Sunday...)
	public static String getDayName(String input){
		try {
			Date dayDate = new SimpleDateFormat("yyyy-MM-dd").parse(input);
			return new SimpleDateFormat("EEEE", Locale.ENGLISH).format(dayDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static int getCurrentHour(){
		Date hourDate = new Date();
		return hourDate.getHours();
	}
	
	public static int getCurrentMinute(){
		Date hourDate = new Date();
		return hourDate.getMinutes();
	}

	public static boolean checkEventExpired(String beginDate){
		try {
			Date dayDate = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			Date today = new SimpleDateFormat("yyyy-MM-dd").parse(getTodaysDate());
			return dayDate.before(today);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean isRedDay(Event event){

		return event.getCourse().contains("*");
	}
	
	public static boolean isRedDay(String eventName){

		return eventName.contains("*");
	}

	public static boolean isInLaterDay(String beginDate){
		try {
			Date dayDate = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
			Date today = new SimpleDateFormat("yyyy-MM-dd").parse(getTodaysDate());
			return dayDate.after(today);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean isReminderAvailable(int reminder, Event event){
		if(!isInLaterDay(event.getBeginDate())){
			try {
				Date beginTime = new SimpleDateFormat("HH:mm").parse(event.getBeginTime());

				int eventHour = 0;
				int eventMinute= 0;
				int temp = 0;
				int reminderHour = 0, reminderMinute = 0;

				SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");

				eventHour = beginTime.getHours();
				eventMinute = beginTime.getMinutes();

				switch (reminder){
				case (60) : {
					if(eventHour!=0){
						reminderHour = eventHour - 1;
						reminderMinute = eventMinute;
					}
					else{
						reminderHour = 23;
						reminderMinute = eventMinute;
					}
					break;
				}
				case (30) : {
					if(eventMinute > 30){
						reminderMinute = eventMinute - 30;
						reminderHour = eventHour;
					}
					else if (eventMinute < 30){
						temp = 30-eventMinute;
						reminderMinute = 60 - temp;
						reminderHour = eventHour - 1;
					}
					else if(eventMinute == 30){
						reminderHour = eventHour;
						reminderMinute = 0;
					}
					break;
				}
				case (15) : {
					if(eventMinute > 15){
						reminderMinute = eventMinute - 15;
						reminderHour = eventHour;
					}
					else if (eventMinute < 15){
						temp = 15 - eventMinute;
						reminderMinute = 60 - temp;
						reminderHour = eventHour - 1;
					}
					else if(eventMinute == 15){
						reminderMinute = 0;
						reminderHour = eventHour;
					}
					break;
				}
				case (5) : {
					if(eventMinute > 5){
						reminderMinute = eventMinute - 5;
						reminderHour = eventHour;
					}
					else if (eventMinute < 5){
						temp = 5 - eventMinute;
						reminderMinute = 60 - temp;
						reminderHour = eventHour - 1;
					}
					else if(eventMinute == 5){
						reminderMinute = 0;
						reminderHour = eventHour;
					}
					break;
				}
				default: break;
				}

				beginTime.setHours(reminderHour);
				beginTime.setMinutes(reminderMinute);

				Date currentDate = new Date();
				Date currentTime = new SimpleDateFormat("HH:mm").parse(hourFormat.format(currentDate));
				//System.out.println("beginH: "+beginTime.getHours()+" beginM: "+beginTime.getMinutes());
				return beginTime.after(currentTime);

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

	public static boolean isEndDateLater(Event event){
		try {
			Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(event.getEndDate());
			Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(event.getBeginDate());
			return endDate.after(beginDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}

	public static int toInt(Object obj){
		return Integer.parseInt(obj.toString());
	}

	public static boolean toBool(Object obj){
		return Boolean.parseBoolean(obj.toString());
	}
	
	public static void initializeColors(){
		colors.add(R.color.OptionBlue);
		colors.add(R.color.OptionDarkBeige);
		colors.add(R.color.OptionDarkPink);
		colors.add(R.color.OptionDarkPurple);
		colors.add(R.color.OptionDarkRed);
		colors.add(R.color.OptionGold);
		colors.add(R.color.OptionIceBlue);
		colors.add(R.color.OptionLightBrown);
		colors.add(R.color.OptionLightGreen);
		colors.add(R.color.OptionLightPurple);
		colors.add(R.color.OptionLightYellow);
		colors.add(R.color.OptionRed);
		colors.add(R.color.OptionVibeBlue);
		colors.add(R.color.OptionWhite);
		
	}
	
	public static void initializeArray(Context mContext){
		visibilityArray = new ArrayList<HashMap<String,String>>();
		eventsNamesList = new ArrayList<String>();
		visibilityArray.clear();
		eventsNamesList.clear();
		datasource = new TimeEditDataSource(mContext);
		datasource.open();
		eventsNamesList = datasource.getAllEventNames();
		datasource.close();
		int colorIndex = 0;
		for (String eventName : eventsNamesList) {
			HashMap<String, String> eventMap = new HashMap<String, String>();
			eventMap.put(COURSENAME, eventName);
			eventMap.put(VISIBILITY, visibilityColorPreferences.getBoolean(eventName+VISIBILITY, true)+"");
			visibilityColorPreferencesEditor.putInt(eventName+COLOR, visibilityColorPreferences.getInt(eventName+COLOR, MyHelper.colors.get(colorIndex))).commit();
			eventMap.put(COLOR, visibilityColorPreferences.getInt(eventName+COLOR, MyHelper.colors.get(colorIndex))+"");
			visibilityArray.add(eventMap);
			if(colorIndex < 13){
				colorIndex++;
			}else{
				colorIndex = 0;
			}
		}
	}
	
	public static void initializeSharedPreferences(Context mContext){
		visibilityColorPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
		visibilityColorPreferencesEditor = visibilityColorPreferences.edit();
	}

}

